package com.zzptc.zzsgz.bdsjws.activities;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.services.IVersionUpdate;
import com.zzptc.zzsgz.bdsjws.utils.MyUtils;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Update2Activity extends BaseActivity {

    @BindView(R.id.iv_one)
    ImageView ivOne;
    @BindView(R.id.iv_two)
    ImageView ivTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getContentXML() {
        return R.layout.activity_update2;
    }

    @Override
    public void initData() {
       //检查网络状态 2是无网    1是有网，但比较慢 窗体关闭 3是网络正常可以下载 4是网络正常已经是最新版本
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://10.0.2.2:8080/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        IVersionUpdate versionUpdate=retrofit.create(IVersionUpdate.class);//创建接口对象
       Call<VersionInfo> version=versionUpdate.getVersion();
        //异步处理
        version.enqueue(new Callback<VersionInfo>() {
            @Override
            public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                //网络正常 3是网络正常可以下载 4是网络正常已经是最新版本
                VersionInfo versionInfo= response.body();//body取到的值
                int newcode=versionInfo.getVersioncode();//得到最新的版本号
                int oldcode= MyUtils.getVersionCode(MyApplication.getContext());
                if(newcode>oldcode){
                  //  setResult(3);//因为要将VersionInfo  传递过去
                    Intent intent=new Intent();
                    intent.putExtra("VersionInfo",versionInfo);
                    setResult(3,intent);//传递方  传递给父类
                    finish();
                }else {
                    setResult(4);
                    finish();
                }
            }

            @Override
            public void onFailure(Call<VersionInfo> call, Throwable t) {
                //网络比较慢  1是有网，但比较慢 窗体关闭
                setResult(1);
                finish();
            }
        });
        boolean b=checkNte();
        if (b){
            //访问服务器取json数据  retrofit 取gson数据 判断版本是否是最新的
        }else {
            setResult(2);//传值方
            finish();
        }
    }


    private boolean checkNte(){
        //网络是怎样的状态
        NetworkInfo.State wifiState = null;
        NetworkInfo.State mobileState = null;
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED == mobileState) {
            // 手机网络连接成功
            Toast.makeText(this, "手机网络连接成功", Toast.LENGTH_SHORT).show();
            return true;
        } else if (wifiState != null && mobileState != null
                && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED != mobileState) {
            // 手机没有任何的网络
            Toast.makeText(this, "手机没有任何的网络", Toast.LENGTH_SHORT).show();
            return false;
        } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
            // 无线网络连接成功
            Toast.makeText(this, "无线网络连接成功", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;

    }


    @Override
    public void intView() {
//动画
        ObjectAnimator animatorY=ObjectAnimator.ofFloat(ivOne,"scaleY",1f,150f,1f);
        ObjectAnimator animatorX=ObjectAnimator.ofFloat(ivOne,"scaleX",1f,150f,1f);
        final AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.play(animatorX).with(animatorY);
        animatorSet.setDuration(5000);
        animatorSet.start();

        ObjectAnimator animator1=ObjectAnimator.ofFloat(ivTwo,"scaleY",1f,150f,1f);
        ObjectAnimator animator2=ObjectAnimator.ofFloat(ivTwo,"scaleX",1f,150f,1f);
      final   AnimatorSet animatorSet2=new AnimatorSet();
        animatorSet2.play(animator2).with(animator1);
        animatorSet2.setDuration(5000);
        //写个监听 第一个动画完成开始第二个动画
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                animatorSet2.start();
            }
        });
 animatorSet2.addListener(new AnimatorListenerAdapter() {
           @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                animatorSet.start();
            }
        });
    }
}
